<?php
namespace app\system\controller;

use app\common\logic\CmsBuildLogic;
use app\common\model\CmsMoudleModel;
use think\helper\Str;
use think\Request;

class CmsBuildMoudleController extends BaseController
{

    private $cmsMoudleModel;
    private $cmsBuildLogic;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->cmsMoudleModel = new CmsMoudleModel();
        $this->cmsBuildLogic  = new CmsBuildLogic();
    }

    /**
     * 模型生成首页
     * @return mixed
     */
    public function index()
    {
        $mid        = $this->request->param('mid', 0);
        $moudleInfo = $this->cmsMoudleModel->get($mid);
        $fileList   = $this->cmsMoudleModel->getBuildList($moudleInfo);
        $this->assign('moudleInfo', $moudleInfo);
        $this->assign('fileList', $fileList);
        return $this->fetch();
    }

    /**
     * 生成验证器
     */
    public function buildValidate()
    {
        if ($this->request->isAjax()) {
            $mid      = $this->request->param('mid');
            $filename = $this->request->param('filename');
            $filepath = $this->request->param('filepath');
            $content  = $this->cmsBuildLogic->buildValidate($mid);
            $result   = file_put_contents($filepath . $filename, '<?php' . PHP_EOL . $content);
            if ($result === false) {
                $this->error('服务器目录不可写');
            } else {
                $this->cmsMoudleModel->where('mid', $mid)->setField('validate_tpl', 1);//更新模型状态
                $this->success('生成成功！', url('index', ['mid' => $mid]));
            }
        }
        $mid        = $this->request->param('mid');
        $moudleInfo = $this->cmsMoudleModel->get($mid);
        $filepath   = 'app/common/validate/';
        $filename   = Str::studly($moudleInfo['table_name']) . 'Validate.php';
        switch ($moudleInfo->validate_tpl) {
            case 0:
                $infoMsg = '验证器文件还未初始化！';
                break;
            case 1:
                $infoMsg = '验证器文件已生成，请谨慎操作！';
                break;
            case 2:
                $infoMsg = '验证器文件已锁定，请解锁后再操作！';
                break;
        }
        $content = htmlspecialchars($this->cmsBuildLogic->buildValidate($mid));
        $this->assign('infoMsg', $infoMsg);
        $this->assign('filepath', $filepath);
        $this->assign('filename', $filename);
        $this->assign('moudleInfo', $moudleInfo);
        $this->assign('content', $content);
        return $this->fetch();
    }

    /**
     * 生成模型
     */
    public function buildModel()
    {
        if ($this->request->isAjax()) {
            $mid      = $this->request->param('mid');
            $filename = $this->request->param('filename');
            $filepath = $this->request->param('filepath');
            $content  = $this->cmsBuildLogic->buildModel($mid);
            $result   = file_put_contents($filepath . $filename, '<?php' . PHP_EOL . $content);
            if ($result === false) {
                $this->error('服务器目录不可写');
            } else {
                $this->cmsMoudleModel->where('mid', $mid)->setField('model_tpl', 1);//更新模型状态
                $this->success('生成成功！', url('index', ['mid' => $mid]));
            }
        }
        $mid        = $this->request->param('mid');
        $moudleInfo = $this->cmsMoudleModel->get($mid);
        $filepath   = 'app/common/model/';
        $filename   = Str::studly($moudleInfo['table_name']) . 'Model.php';
        switch ($moudleInfo->model_tpl) {
            case 0:
                $infoMsg = '模型文件还未初始化！';
                break;
            case 1:
                $infoMsg = '模型文件已生成，请谨慎操作！';
                break;
            case 2:
                $infoMsg = '模型文件已锁定，请解锁后再操作！';
                break;
        }
        $content = htmlspecialchars($this->cmsBuildLogic->buildModel($mid));
        $this->assign('infoMsg', $infoMsg);
        $this->assign('filepath', $filepath);
        $this->assign('filename', $filename);
        $this->assign('moudleInfo', $moudleInfo);
        $this->assign('content', $content);
        return $this->fetch();
    }

    /**
     * 设置锁定状态
     */
    public function setTplStatus()
    {
        $mid      = $this->request->param('mid');
        $tplField = $this->request->param('tpl_field');
        $tplValue = $this->request->param('tpl_value');
        $this->cmsMoudleModel->where('mid', $mid)->setField($tplField, $tplValue);
        $this->success('操作成功！');
    }

}
